/*
 * @Author: 徐博
 * @Date: 2023-02-16 17:30:41
 * @Last Modified by: 徐博
 * @Last Modified time: 2023-03-07 17:42:56
 */

import { defineStore } from 'pinia'
import { createPinia } from 'pinia';
import piniaPersist from 'pinia-plugin-persist';
import { UserInfo } from '@/type/user-type';

interface GlobalState {
    token: string;
    userInfo: UserInfo;
}
// defineStore 调用后返回一个函数，调用该函数获得Store实体
export const GlobalState = defineStore({
    id: 'GlobalState',
    //state: 返回对象的函数
    state: (): GlobalState => ({
        token: '',
        userInfo: {
            username: '',
            mobile: 0,
            id: 0,
            permissions_btn: ['userList,delete', 'userList.view'], //按钮权限
            permissions_menu: ['/', '/userList1'], //菜单权限
        },
    }),
    getters: {},
    actions: {
        setToken(token: string) {
            this.token = token;
        },
        setUserInfo(userInfo: UserInfo) {
            this.userInfo = userInfo
        },
    },
    //持久化
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'GlobalState',
                storage: localStorage,
            }
        ]
    }
})

const pinia = createPinia();
pinia.use(piniaPersist);

export default pinia;
